package l1j.server.server.model;

import static l1j.server.server.model.skill.L1SkillId.BLIND_HIDING;
import static l1j.server.server.model.skill.L1SkillId.COUNTER_BARRIER;
import static l1j.server.server.model.skill.L1SkillId.DETECTION;
import static l1j.server.server.model.skill.L1SkillId.ENCHANT_WEAPON;
import static l1j.server.server.model.skill.L1SkillId.EXTRA_HEAL;
import static l1j.server.server.model.skill.L1SkillId.GREATER_HASTE;
import static l1j.server.server.model.skill.L1SkillId.HASTE;
import static l1j.server.server.model.skill.L1SkillId.HEAL;
import static l1j.server.server.model.skill.L1SkillId.INVISIBILITY;
import static l1j.server.server.model.skill.L1SkillId.PHYSICAL_ENCHANT_DEX;
import static l1j.server.server.model.skill.L1SkillId.PHYSICAL_ENCHANT_STR;
import static l1j.server.server.model.skill.L1SkillId.STATUS_BRAVE;

import java.util.ArrayList;

import l1j.opqlo.NewSystem.ItemLevelUp.LevelStatus;
import l1j.server.server.datatables.lock.CharSkillReading;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.skill.L1BuffUtil;
import l1j.server.server.serverpackets.S_Ability;
import l1j.server.server.serverpackets.S_AddSkill;
import l1j.server.server.serverpackets.S_DelSkill;
import l1j.server.server.serverpackets.S_Invis;
import l1j.server.server.serverpackets.S_RemoveObject;
import l1j.server.server.serverpackets.S_SPMR;
import l1j.server.server.serverpackets.S_SkillBrave;
import l1j.server.server.serverpackets.S_SkillHaste;
import l1j.server.server.templates.L1Item;

public class L1EquipmentSlot {
	private final L1PcInstance _owner;

	/**
	 * 効果中のセットアイテム
	 */
	private final ArrayList<L1ArmorSet> _currentArmorSet;

	private L1ItemInstance _weapon;
	private final ArrayList<L1ItemInstance> _armors;

	public L1EquipmentSlot(final L1PcInstance owner) {
		this._owner = owner;

		this._armors = new ArrayList<L1ItemInstance>();
		this._currentArmorSet = new ArrayList<L1ArmorSet>();
	}

	public ArrayList<L1ItemInstance> getArmors() {
		return this._armors;
	}

	public L1ItemInstance getWeapon() {
		return this._weapon;
	}

	//TODO 飾品強化卷軸
	public void remove(final L1ItemInstance equipment) {
		final L1Item item = equipment.getItem();
		if (item.getType2() == 0) {
			return;
		}

		final LevelStatus lvs = equipment.getLvStatus();

		/**opqlo 屬性相剋*/
		if(lvs.AttrIsNull() == false){
			final double eath = lvs.getAttrDmg(1);
			if(eath>0){
				this._owner.addAttrDmg(1,-eath);
			}
			final double fire = lvs.getAttrDmg(2);
			if(fire >0){
				this._owner.addAttrDmg(2, -fire);
			}
			final double water = lvs.getAttrDmg(4);
			if(water >0){
				this._owner.addAttrDmg(4, -water);
			}
			final double light = lvs.getAttrDmg(5);
			if(light >0){
				this._owner.addAttrDmg(5, -light);
			}
			final double dark = lvs.getAttrDmg(6);
			if(dark >0){
				this._owner.addAttrDmg(6, -dark);
			}
			final double wind = lvs.getAttrDmg(8);
			if(wind >0){
				this._owner.addAttrDmg(8, -wind);
			}
		}

		if(!lvs.getAttrDef().isEmpty()){
			final int[] keylist = {1,2,4,5,6,8};
			for(final int key : keylist){
				if(lvs.getAttrDef().containsKey(key)){
					this._owner.addAttrDef(key, -lvs.getAttrDef().get(key));
				}
			}
		}

		/**opqlo 種族相剋 0=普通、1=不死系、2=惡魔系、3=BOSS系、4=亡靈系、5=狼人系、6=龍系*/
		if(lvs.EthnicityIsNull() == false){
			final double normal = lvs.getEthnicityDmg(0);
			if(normal >0){
				this._owner.addEthncityDmg(0, -normal);
			}
			final double undead = lvs.getEthnicityDmg(1);
			if(undead >0){
				this._owner.addEthncityDmg(1, -undead);
			}
			final double diablo = lvs.getEthnicityDmg(2);
			if(diablo >0){
				this._owner.addEthncityDmg(2, -diablo);
			}
			final double boss = lvs.getEthnicityDmg(3);
			if(boss >0){
				this._owner.addEthncityDmg(3, -boss);
			}
			final double ghost = lvs.getEthnicityDmg(4);
			if(ghost >0){
				this._owner.addEthncityDmg(4, -ghost);
			}
			final double wolf = lvs.getEthnicityDmg(5);
			if(wolf >0){
				this._owner.addEthncityDmg(5, -wolf);
			}
			final double dragon = lvs.getEthnicityDmg(6);
			if(dragon >0){
				this._owner.addEthncityDmg(6, -dragon);
			}
			final double human = lvs.getAttrDmg(7);
			if(human >0){
				this._owner.addEthncityDmg(7, -human);
			}
		}
		final double bigdmg = equipment.getBigDmg() / 100D;
		if(bigdmg>0){
			this._owner.setBigDmg(-bigdmg);
		}
		final double smalldmg = equipment.getSmallDmg() / 100D;
		if(smalldmg>0){
			this._owner.setSmallDmg(-smalldmg);
		}
		final double exprate = equipment.getExpUp();
		if(exprate >0){
			this._owner.addExpUp(-exprate);
		}
		if (item.get_addhp() != 0 || equipment.getaddHp() != 0  || lvs.getLvHp()!=0) {
			this._owner.addMaxHp(-equipment.getaddHp() - lvs.getLvHp() -item.get_addhp());
		}
		if (item.get_addmp() != 0 ||equipment.getaddMp() != 0 || lvs.getLvMp()!=0) {
			this._owner.addMaxMp(-equipment.getaddMp() - lvs.getLvMp() -item.get_addmp());
		}
		this._owner.addMagicDmgModfier(-lvs.getLvMagicDmgModifier());

		this._owner.addStunDice(-equipment.getStunDice());
		this._owner.addStoneDice(-equipment.getStoneDice());
		this._owner.addSleepDice(-equipment.getSleepDice());
		this._owner.addFreezeDice(-equipment.getFreezeDice());
		this._owner.addSustainDice(-equipment.getSustainDice());
		this._owner.addBlindDice(-equipment.getBlindDice());
		this._owner.addRegistStun(-equipment.getStunMr());
		this._owner.addRegistStone(-equipment.getStoneMr());
		this._owner.addRegistSleep(-equipment.getSleepMr());
		this._owner.addRegistFreeze(-equipment.getFreezeMr());
		this._owner.addRegistSustain(-equipment.getSustainMr());
		this._owner.addRegistBlind(-equipment.getBlindMr());
		//TODO 飾品強化卷軸
		this._owner.addEarth(-equipment.getEarthMr());
		this._owner.addWind(-equipment.getWindMr());
		this._owner.addWater(-equipment.getWaterMr());
		this._owner.addFire(-equipment.getFireMr());
		//TODO 飾品強化卷軸
		//opqlo 道具升級系統START
		//TODO 飾品強化卷軸
		/**opqlo 六屬性加成取得*/
		int str = 0;
		int dex = 0;
		int Int = 0;
		if(equipment.getItemId() == 810004){
			final int value = Math.min(150, equipment.getLevel());
			str = value;
			dex = value;
			Int = value;
		}
		this._owner.addStr( - (item.get_addstr()+lvs.getLvStr()+lvs.getStatus(0) +str));
		this._owner.addDex( - (item.get_adddex()+lvs.getLvDex()+lvs.getStatus(1) +dex));
		this._owner.addInt( - (item.get_addint()+lvs.getLvInt()+lvs.getStatus(2) +Int));
		this._owner.addWis( - (item.get_addwis()+lvs.getLvWis()+lvs.getStatus(3) ));
		this._owner.addCon( - (item.get_addcon()+lvs.getLvCon()+lvs.getStatus(4) ));
		if (item.get_addwis() != 0 || lvs.getLvWis()+lvs.getStatus(3) != 0) {
			this._owner.resetBaseMr();
		}
		//opqlo 道具升級系統END

		this._owner.addCha( -( item.get_addcha()+lvs.getStatus(5) ));


		//opqlo 道具升級系統START
		final int ExtraDmg = item.getExtraDmg()+lvs.getLvExtraDmg()+equipment.getExtraDmg();
		final int ExtraDef = item.getExtraDef()+lvs.getLvExtraDef()+equipment.getExtraDef();
		if(ExtraDmg !=0){
			this._owner.addExtraDmg(-ExtraDmg);//opqlo 額外攻擊力
		}
		if(ExtraDef !=0){
			this._owner.addExtraDef(-ExtraDef);//opqlo 額外防禦力
		}

		int addMr = 0;
		addMr -= equipment.getMr()+lvs.getLvMR()+equipment.getaddMr();
		if (item.getItemId() == 20236 && this._owner.isElf()) {
			addMr -= 5;
		}
		if (addMr != 0) {//魔防
			this._owner.addMr(addMr);
			this._owner.sendPackets(new S_SPMR(this._owner));
		}
		int addSp =0;
		addSp -= item.get_addsp()+lvs.getLvSP()+equipment.getaddSp();
		if (addSp != 0) {
			this._owner.addSp(addSp);
			this._owner.sendPackets(new S_SPMR(this._owner));
		}
		//opqlo 道具升級系統END

		/**opqlo 掉寶率*/
		final double droprate = equipment.getDroprate() + item.getDroprate() + lvs.get掉寶率();

		if(droprate !=0){
			this._owner.addDroprate(-droprate);
		}
		/**opqlo 金幣率*/

		final double moneyrate = lvs.get金幣率();

		if(moneyrate !=0){
			this._owner.add金幣率(-moneyrate);
		}
		if (item.isHasteItem()) {
			this._owner.addHasteItemEquipped(-1);
			if (this._owner.getHasteItemEquipped() == 0) {
				this._owner.setMoveSpeed(0);
				this._owner.sendPackets(new S_SkillHaste(this._owner.getId(), 0, 0));
				this._owner.broadcastPacketAll(new S_SkillHaste(this._owner.getId(), 0, 0));
			}
		}
		this._owner.getEquipSlot().removeMagicHelm(this._owner.getId(), equipment);
		final int type2 = item.getType2();
		if (type2 == 1) {
			this.removeWeapon(equipment);
		} else if (type2 == 2) {
			this.removeArmor(equipment);
		}
	}

	private void removeArmor(final L1ItemInstance armor) {
		final L1Item item = armor.getItem();
		final int itemId = armor.getItem().getItemId();

		//TODO 飾品不加防判斷
		final int type2 = armor.getItem().getType2();
		final int type = armor.getItem().getType();
		if (type2 == 2 && type >= 8 && type <= 12) {
			this._owner.addAc(-(item.get_ac() - armor.getAcByMagic()));
		} else {
			this._owner.addAc(-(item.get_ac() - armor.getEnchantLevel() - armor.getAcByMagic()));
		}

		//TODO 飾品不加防判斷

		final int dmgre = (int) (1.3* Math.max(0, armor.getEnchantLevel() - item.get_safeenchant()));

		this._owner.addDamageReductionByArmor(-(item.getDamageReduction()+armor.getLvStatus().getLvDmgReduction()+dmgre));
		this._owner.addWeightReduction(-item.getWeightReduction());
		this._owner.addHitModifierByArmor(-(item.getHitModifierByArmor()+armor.getLvStatus().getLvHitModifier() ));
		this._owner.addDmgModifierByArmor(-(item.getDmgModifierByArmor()+armor.getLvStatus().getLvDmgModifier()) );
		this._owner.addBowHitModifierByArmor(-item.getBowHitModifierByArmor());
		this._owner.addBowDmgModifierByArmor(-item.getBowDmgModifierByArmor());
		/*
	    _owner.addEarth(-item.get_defense_earth());
		_owner.addWind(-item.get_defense_wind());
		_owner.addWater(-item.get_defense_water());
		_owner.addFire(-item.get_defense_fire());
		 */
		this._owner.addRegistStun(-item.get_regist_stun());
		this._owner.addRegistStone(-item.get_regist_stone());
		this._owner.addRegistSleep(-item.get_regist_sleep());
		this._owner.addRegistFreeze(-item.get_regist_freeze());
		this._owner.addRegistSustain(-item.get_regist_sustain());
		this._owner.addRegistBlind(-item.get_regist_blind());
		//TODO 飾品強化卷軸
		this._owner.addEarth(-item.get_defense_earth() - armor.getEarthMr());
		this._owner.addWind(-item.get_defense_wind() - armor.getWindMr());
		this._owner.addWater(-item.get_defense_water() - armor.getWaterMr());
		this._owner.addFire(-item.get_defense_fire() - armor.getFireMr());
		//TODO 飾品強化卷軸

		/**opqlo 種族相剋*/
		/*if(ItemLevelUpTable.getInstance().getTemplate(armor.getItemId() )==null){ //opqlo 不在升級清單中
		_owner.setSpAttack(armor.getSkillType(),- armor.getLevel());//opqlo 種族相剋
		}*/

		for (final L1ArmorSet armorSet : L1ArmorSet.getAllSet()) {
			if (armorSet.isPartOfSet(itemId)
					&& this._currentArmorSet.contains(armorSet)
					&& !armorSet.isValid(this._owner)) {
				if (armor.getItem().getType2() == 2
						&& armor.getItem().getType() == 9) { // ring
					if (!armorSet.isEquippedRingOfArmorSet(this._owner)) {//opqlo 修正戒指套裝設定
						armorSet.cancelEffect(this._owner);
						this._currentArmorSet.remove(armorSet);
					}
				}else{
					armorSet.cancelEffect(this._owner);
					this._currentArmorSet.remove(armorSet);
				}
			}else if(armorSet.isPartOfSet(itemId)
					&& this._currentArmorSet.contains(armorSet)
					&& armorSet.isValid(this._owner)){
				if (armor.getItem().getType2() == 2
						&& armor.getItem().getType() == 9) { // ring
					if (!armorSet.isEquippedRingOfArmorSet(this._owner)) {//opqlo 修正戒指套裝設定
						armorSet.cancelEffect(this._owner);
						this._currentArmorSet.remove(armorSet);
					}
				}else{
					armorSet.cancelEffect(this._owner);
					this._currentArmorSet.remove(armorSet);
				}
			}
		}
		if(item.getItemId() == 800000){//opqlo VIP
			this._owner.isVIP =false;
			this._owner.add金幣率(-0.5);//-50%

		}else if (itemId == 20077 || itemId == 20062 || itemId == 120077) {
			this._owner.delInvis(); // インビジビリティ状態解除

		}else if (itemId == 20288) { // ROTC
			this._owner.sendPackets(new S_Ability(1, false));
		}
		armor.stopEquipmentTimer(this._owner);
		this._armors.remove(armor);
		this._owner.StripArmor(item.getType()); //opqlo 裝備列表
	}

	/**
	 * 解除(魔法道具)
	 * @param objectId
	 * @param item
	 */
	public void removeMagicHelm(final int objectId, final L1ItemInstance item) {
		switch (item.getItemId()) {
		case 20013: // 敏捷魔法頭盔
			if (!CharSkillReading.get().spellCheck(objectId, PHYSICAL_ENCHANT_DEX)) {
				this._owner.sendPackets(new S_DelSkill(this._owner, PHYSICAL_ENCHANT_DEX));
			}
			if (!CharSkillReading.get().spellCheck(objectId, HASTE)) {
				this._owner.sendPackets(new S_DelSkill(this._owner, HASTE));
			}
			break;

		case 20014: // 治癒魔法頭盔
			if (!CharSkillReading.get().spellCheck(objectId, HEAL)) {
				this._owner.sendPackets(new S_DelSkill(this._owner, HEAL));
			}
			if (!CharSkillReading.get().spellCheck(objectId, EXTRA_HEAL)) {
				this._owner.sendPackets(new S_DelSkill(this._owner, EXTRA_HEAL));
			}
			break;

		case 20015: // 力量魔法頭盔
			if (!CharSkillReading.get().spellCheck(objectId, ENCHANT_WEAPON)) {
				this._owner.sendPackets(new S_DelSkill(this._owner, ENCHANT_WEAPON));
			}
			if (!CharSkillReading.get().spellCheck(objectId, DETECTION)) {
				this._owner.sendPackets(new S_DelSkill(this._owner, DETECTION));
			}
			if (!CharSkillReading.get().spellCheck(objectId, PHYSICAL_ENCHANT_STR)) {
				this._owner.sendPackets(new S_DelSkill(this._owner, PHYSICAL_ENCHANT_STR));
			}
			break;

		case 20008: // 小型風之頭盔
			if (!CharSkillReading.get().spellCheck(objectId, HASTE)) {
				this._owner.sendPackets(new S_DelSkill(this._owner, HASTE));
			}
			break;

		case 20023: // 風之頭盔
			if (!CharSkillReading.get().spellCheck(objectId, GREATER_HASTE)) {
				this._owner.sendPackets(new S_DelSkill(this._owner, GREATER_HASTE));
			}
			break;

		default:// 其他道具
			break;
		}
	}

	private void removeWeapon(final L1ItemInstance weapon) {
		this._owner.setWeapon(null);
		this._owner.setCurrentWeapon(0);
		this._owner.addMagicDmgModfier(-weapon.getItem().getMagicDmgModifier());
		weapon.stopEquipmentTimer(this._owner);
		this._weapon = null;
		if (this._owner.hasSkillEffect(COUNTER_BARRIER)) {
			this._owner.removeSkillEffect(COUNTER_BARRIER);
		}
	}
	//TODO 飾品強化卷軸
	public void set(final L1ItemInstance equipment) {
		final L1Item item = equipment.getItem();
		if (item.getType2() == 0) {
			return;
		}

		final LevelStatus lvs = equipment.getLvStatus();

		final int addhp = item.get_addhp()+ equipment.getaddHp() + lvs.getLvHp();
		final int addmp = item.get_addmp()+ equipment.getaddMp() + lvs.getLvMp();
		if(addhp !=0){
			this._owner.addMaxHp(addhp);
		}
		if(addmp !=0){
			this._owner.addMaxMp(addmp);
		}

		/**opqlo 屬性相剋*/
		final double eath = lvs.getAttrDmg(1);
		if(eath>0){
			this._owner.addAttrDmg(1,eath);
		}
		final double fire = lvs.getAttrDmg(2);
		if(fire >0){
			this._owner.addAttrDmg(2, fire);
		}
		final double water = lvs.getAttrDmg(4);
		if(water >0){
			this._owner.addAttrDmg(4, water);
		}
		final double light = lvs.getAttrDmg(5);
		if(light >0){
			this._owner.addAttrDmg(5, light);
		}
		final double dark = lvs.getAttrDmg(6);
		if(dark >0){
			this._owner.addAttrDmg(6, dark);
		}
		final double wind = lvs.getAttrDmg(8);
		if(wind >0){
			this._owner.addAttrDmg(8, wind);
		}
		/**opqlo 種族相剋 0=普通、1=不死系、2=惡魔系、3=BOSS系、4=亡靈系、5=狼人系、6=龍系*/
		if(lvs.EthnicityIsNull()==false){
			final double normal = lvs.getEthnicityDmg(0);
			if(normal >0){
				this._owner.addEthncityDmg(0, normal);
			}
			final double undead = lvs.getEthnicityDmg(1);
			if(undead >0){
				this._owner.addEthncityDmg(1, undead);
			}
			final double diablo = lvs.getEthnicityDmg(2);
			if(diablo >0){
				this._owner.addEthncityDmg(2, diablo);
			}
			final double boss = lvs.getEthnicityDmg(3);
			if(boss >0){
				this._owner.addEthncityDmg(3, boss);
			}
			final double ghost = lvs.getEthnicityDmg(4);
			if(ghost >0){
				this._owner.addEthncityDmg(4, ghost);
			}
			final double wolf = lvs.getEthnicityDmg(5);
			if(wolf >0){
				this._owner.addEthncityDmg(5, wolf);
			}
			final double dragon = lvs.getEthnicityDmg(6);
			if(dragon >0){
				this._owner.addEthncityDmg(6, dragon);
			}
			final double human = lvs.getAttrDmg(7);
			if(human >0){
				this._owner.addEthncityDmg(7, human);
			}
		}
		if(!lvs.getAttrDef().isEmpty()){
			final int[] keylist = {1,2,4,5,6,8};
			for(final int key : keylist){
				if(lvs.getAttrDef().containsKey(key)){
					this._owner.addAttrDef(key, lvs.getAttrDef().get(key));
				}
			}
		}

		final double bigdmg = equipment.getBigDmg() / 100D;
		if(bigdmg>0){
			this._owner.setBigDmg(bigdmg);
		}
		final double smalldmg = equipment.getSmallDmg() / 100D;
		if(smalldmg>0){
			this._owner.setSmallDmg(smalldmg);
		}
		final double exprate = equipment.getExpUp();
		if(exprate >0){
			this._owner.addExpUp(exprate);
		}

		this._owner.addMagicDmgModfier(lvs.getLvMagicDmgModifier());
		this._owner.addStunDice(equipment.getStunDice());
		this._owner.addStoneDice(equipment.getStoneDice());
		this._owner.addSleepDice(equipment.getSleepDice());
		this._owner.addFreezeDice(equipment.getFreezeDice());
		this._owner.addSustainDice(equipment.getSustainDice());
		this._owner.addBlindDice(equipment.getBlindDice());

		this._owner.addRegistStun(equipment.getStunMr());
		this._owner.addRegistStone(equipment.getStoneMr());
		this._owner.addRegistSleep(equipment.getSleepMr());
		this._owner.addRegistFreeze(equipment.getFreezeMr());
		this._owner.addRegistSustain(equipment.getSustainMr());
		this._owner.addRegistBlind(equipment.getBlindMr());
		//TODO 飾品強化卷軸
		this._owner.addEarth(equipment.getEarthMr());
		this._owner.addWind(equipment.getWindMr());
		this._owner.addWater(equipment.getWaterMr());
		this._owner.addFire(equipment.getFireMr());
		//TODO 飾品強化卷軸

		//opqlo 道具升級系統START
		//TODO 飾品強化卷軸
		/**opqlo 六屬性加成取得*/
		int str = 0;
		int dex = 0;
		int Int = 0;
		if(equipment.getItemId() == 810004){
			final int value =  Math.min(150, equipment.getLevel());
			str = value;
			dex = value;
			Int = value;
		}
		this._owner.addStr( item.get_addstr()+lvs.getLvStr()+lvs.getStatus(0) +str);
		this._owner.addDex(  item.get_adddex()+lvs.getLvDex()+lvs.getStatus(1) +dex);
		this._owner.addInt(  item.get_addint()+lvs.getLvInt()+lvs.getStatus(2) +Int);
		this._owner.addWis(  item.get_addwis()+lvs.getLvWis()+lvs.getStatus(3));
		this._owner.addCon(  item.get_addcon()+lvs.getLvCon()+lvs.getStatus(4));
		if (item.get_addwis() != 0 || lvs.getLvWis()+lvs.getStatus(3) != 0) {
			this._owner.resetBaseMr();
		}
		//opqlo 道具升級系統END

		this._owner.addCha( item.get_addcha()+lvs.getStatus(5));


		//opqlo 道具升級系統START
		final int ExtraDmg = item.getExtraDmg()+lvs.getLvExtraDmg()+equipment.getExtraDmg();
		final int ExtraDef = item.getExtraDef()+lvs.getLvExtraDef()+equipment.getExtraDef();
		if(ExtraDmg !=0){
			this._owner.addExtraDmg(ExtraDmg);//opqlo 額外攻擊力
		}
		if(ExtraDef !=0){
			this._owner.addExtraDef(ExtraDef);//opqlo 額外防禦力
		}
		int addMr = 0;
		addMr += equipment.getMr()+lvs.getLvMR()+equipment.getaddMr();
		if (item.getItemId() == 20236 && this._owner.isElf()) {
			addMr += 5;
		}
		if (addMr != 0) {//魔防
			this._owner.addMr(addMr);
			this._owner.sendPackets(new S_SPMR(this._owner));
		}
		int addSp = 0;
		addSp += item.get_addsp()+lvs.getLvSP()+equipment.getaddSp();
		if (addSp != 0) {//魔攻
			this._owner.addSp(addSp);
			this._owner.sendPackets(new S_SPMR(this._owner));
		}
		//opqlo 道具升級系統END

		/**opqlo 掉寶率*/
		final double droprate = equipment.getDroprate() + item.getDroprate() + lvs.get掉寶率();

		if(droprate !=0){
			this._owner.addDroprate(droprate);
		}
		/**opqlo 金幣率*/

		final double moneyrate = lvs.get金幣率();

		if(moneyrate !=0){
			this._owner.add金幣率(moneyrate);
		}


		if (item.isHasteItem()) {
			L1BuffUtil.supperSpeed(this._owner, 0, (byte) 5);//opqlo 3段加速
			this._owner.addHasteItemEquipped(1);
			this._owner.removeHasteSkillEffect();
			if (this._owner.getMoveSpeed() != 1) {
				this._owner.setMoveSpeed(1);
				this._owner.sendPackets(new S_SkillHaste(this._owner.getId(), 1, -1));
				this._owner.broadcastPacketAll(new S_SkillHaste(this._owner.getId(), 1, 0));
			}
		}else if (item.getItemId() == 20383) { // 騎馬用ヘルム
			L1BuffUtil.supperSpeed(this._owner, 0, (byte) 5);//opqlo 3段加速
			if (this._owner.hasSkillEffect(STATUS_BRAVE)) {
				this._owner.killSkillEffectTimer(STATUS_BRAVE);
				this._owner.sendPackets(new S_SkillBrave(this._owner.getId(), 0, 0));
				this._owner.broadcastPacketAll(new S_SkillBrave(this._owner.getId(), 0, 0));
				this._owner.setBraveSpeed(0);
			}
		}
		this._owner.getEquipSlot().setMagicHelm(equipment);

		if (item.getType2() == 1) {
			this.setWeapon(equipment);
		} else if (item.getType2() == 2) {
			this.setArmor(equipment);
			this._owner.sendPackets(new S_SPMR(this._owner));
		}
	}
	private void setArmor(final L1ItemInstance armor) {
		final L1Item item = armor.getItem();
		final int itemId = armor.getItem().getItemId();

		//TODO 飾品不加防判斷
		final int type2 = armor.getItem().getType2();
		final int type = armor.getItem().getType();
		if (type2 == 2 && type >= 8 && type <= 12) {
			this._owner.addAc(item.get_ac() - armor.getAcByMagic());
		} else {
			this._owner.addAc(item.get_ac() - armor.getEnchantLevel()- armor.getAcByMagic());
		}


		//TODO 飾品不加防判斷

		final int dmgre = (int) (1.3*Math.max(0, armor.getEnchantLevel() - item.get_safeenchant()));

		this._owner.addDamageReductionByArmor(item.getDamageReduction()+armor.getLvStatus().getLvDmgReduction() +dmgre);
		this._owner.addWeightReduction(item.getWeightReduction());
		this._owner.addHitModifierByArmor(item.getHitModifierByArmor()+armor.getLvStatus().getLvHitModifier());
		this._owner.addDmgModifierByArmor(item.getDmgModifierByArmor()+armor.getLvStatus().getLvDmgModifier());
		this._owner.addBowHitModifierByArmor(item.getBowHitModifierByArmor());
		this._owner.addBowDmgModifierByArmor(item.getBowDmgModifierByArmor());
		/*
		_owner.addEarth(item.get_defense_earth());
		_owner.addWind(item.get_defense_wind());
		_owner.addWater(item.get_defense_water());
		_owner.addFire(item.get_defense_fire());
		 */
		this._owner.addRegistStun(item.get_regist_stun());
		this._owner.addRegistStone(item.get_regist_stone());
		this._owner.addRegistSleep(item.get_regist_sleep());
		this._owner.addRegistFreeze(item.get_regist_freeze());
		this._owner.addRegistSustain(item.get_regist_sustain());
		this._owner.addRegistBlind(item.get_regist_blind());
		//TODO 飾品強化卷軸
		this._owner.addEarth(item.get_defense_earth() + armor.getEarthMr());
		this._owner.addWind(item.get_defense_wind() + armor.getWindMr());
		this._owner.addWater(item.get_defense_water() + armor.getWaterMr());
		this._owner.addFire(item.get_defense_fire() + armor.getFireMr());
		//TODO 飾品強化卷軸
		/*if(ItemLevelUpTable.getInstance().getTemplate(armor.getItemId() )==null){ //opqlo 不在升級清單中
		_owner.setSpAttack(armor.getSkillType(), armor.getLevel());//opqlo 種族相剋
		}*/

		this._armors.add(armor);

		for (final L1ArmorSet armorSet : L1ArmorSet.getAllSet()) {
			if (armorSet.isPartOfSet(itemId) && armorSet.isValid(this._owner)) {
				if (armor.getItem().getType2() == 2
						&& armor.getItem().getType() == 9) { // ring

					//if (!armorSet.isEquippedRingOfArmorSet(_owner)) {//opqlo 修正戒指套裝設定
					if (armorSet.isEquippedRingOfArmorSet(this._owner)) {//opqlo 修正戒指套裝設定
						armorSet.giveEffect(this._owner);
						this._currentArmorSet.add(armorSet);

					}

				} else {
					armorSet.giveEffect(this._owner);
					this._currentArmorSet.add(armorSet);
				}
			}
		}
		if(itemId == 800000){//opqlo VIP
			this._owner.isVIP =true;
			this._owner.add金幣率(0.5);//+50%



		}else if (itemId == 20077 || itemId == 20062 || itemId == 120077) {
			if (!this._owner.hasSkillEffect(INVISIBILITY)) {
				this._owner.killSkillEffectTimer(BLIND_HIDING);
				this._owner.setSkillEffect(INVISIBILITY, 0);
				this._owner.sendPackets(new S_Invis(this._owner.getId(), 1));
				this._owner.broadcastPacketForFindInvis(new S_RemoveObject(this._owner),false);
				// _owner.broadcastPacket(new S_RemoveObject(_owner));
			}

		}else if (itemId == 20288) { // ROTC
			this._owner.sendPackets(new S_Ability(1, true));

		}else if (itemId == 20383) { // 騎馬用ヘルム
			if (armor.getChargeCount() != 0) {
				armor.setChargeCount(armor.getChargeCount() - 1);
				this._owner.getInventory().updateItem(armor,L1PcInventory.COL_CHARGE_COUNT);
			}
		}
		armor.startEquipmentTimer(this._owner);
		this._owner.WearArmor(armor, item.getType()); //opqlo 裝備列表
	}

	/**
	 * 設置(魔法道具)
	 * @param item
	 */
	public void setMagicHelm(final L1ItemInstance item) {
		switch (item.getItemId()) {
		case 20013: // 敏捷魔法頭盔
			if (!this._owner.isSkillMastery(PHYSICAL_ENCHANT_DEX)) {
				this._owner.sendPackets(new S_AddSkill(this._owner, PHYSICAL_ENCHANT_DEX));
			}
			if (!this._owner.isSkillMastery(HASTE)) {
				this._owner.sendPackets(new S_AddSkill(this._owner, HASTE));
			}
			break;

		case 20014: // 治癒魔法頭盔
			if (!this._owner.isSkillMastery(HEAL)) {
				this._owner.sendPackets(new S_AddSkill(this._owner, HEAL));
			}
			if (!this._owner.isSkillMastery(EXTRA_HEAL)) {
				this._owner.sendPackets(new S_AddSkill(this._owner, EXTRA_HEAL));
			}
			break;

		case 20015: // 力量魔法頭盔
			if (!this._owner.isSkillMastery(ENCHANT_WEAPON)) {
				this._owner.sendPackets(new S_AddSkill(this._owner, ENCHANT_WEAPON));
			}
			if (!this._owner.isSkillMastery(DETECTION)) {
				this._owner.sendPackets(new S_AddSkill(this._owner, DETECTION));
			}
			if (!this._owner.isSkillMastery(PHYSICAL_ENCHANT_STR)) {
				this._owner.sendPackets(new S_AddSkill(this._owner, PHYSICAL_ENCHANT_STR));
			}
			break;

		case 20008: // 小型風之頭盔
			if (!this._owner.isSkillMastery(HASTE)) {
				this._owner.sendPackets(new S_AddSkill(this._owner, HASTE));
			}
			break;

		case 20023: // 風之頭盔
			if (!this._owner.isSkillMastery(GREATER_HASTE)) {
				this._owner.sendPackets(new S_AddSkill(this._owner, GREATER_HASTE));
			}
			break;
		default:// 其他道具
			break;
		}
	}

	private void setWeapon(final L1ItemInstance weapon) {
		this._owner.setWeapon(weapon);
		this._owner.addMagicDmgModfier(weapon.getItem().getMagicDmgModifier());
		this._owner.setCurrentWeapon(weapon.getItem().getType1());
		weapon.startEquipmentTimer(this._owner);
		this._weapon = weapon;
	}

}
